﻿Imports System.Data.Common
Imports ConexionBD

Public Class MComprobante
    Public COD_CPB, COD_MES, COD_TIPO_CPB, _
        COD_USUARIO As String
    Public FECHA_CPB, FECHA_REGISTRO As Date
    Public ID_CPB, CREDITO, DEBITO, ITEMS As Decimal
    Public ESTADO As Char

    Public Shared Function ConsPrimero() As DataTable
        Dim conexion As New ConexionBD.ConexionBD()
        Dim dtConsulta As New DataTable
        If conexion.Conectar() Then
            Dim sentencia As String
            sentencia = "SELECT TOP 1 * FROM MCOMPROBANTE ORDER BY COD_CPB ASC"
            dtConsulta = conexion.Consulta(sentencia)
            conexion.Desconectar()
        End If
        Return dtConsulta
    End Function

    Public Shared Function ConsUltimo() As DataTable
        Dim conexion As New ConexionBD.ConexionBD()
        Dim dtConsulta As New DataTable
        If conexion.Conectar() Then
            Dim sentencia As String
            sentencia = "SELECT TOP 1 * FROM MCOMPROBANTE ORDER BY COD_CPB DESC"
            dtConsulta = conexion.Consulta(sentencia)
            conexion.Desconectar()
        End If
        Return dtConsulta
    End Function

    Public Shared Function ConsSiguiente(mComp As MComprobante) As DataTable
        Dim conexion As New ConexionBD.ConexionBD()
        Dim dtConsulta As New DataTable
        If conexion.Conectar() Then
            Dim sentencia As String
            sentencia = "SELECT TOP 1 * FROM MCOMPROBANTE WHERE COD_CPB=" + _
                "(SELECT TOP 1 COD_CPB FROM MCOMPROBANTE WHERE COD_CPB>@COD_CPB " + _
                "GROUP BY COD_CPB ORDER BY COD_CPB ASC)"
            conexion.ParametroEntrada = New List(Of DbParameter)
            conexion.ParametroEntrada.Add(ConexionBD.ConexionBD.ParamInString(conexion, _
                                                                              mComp.COD_CPB, "COD_CPB"))
            dtConsulta = conexion.Consulta(sentencia)
            conexion.Desconectar()
        End If
        Return dtConsulta
    End Function

    Public Shared Function ConsAnterior(mComp As MComprobante) As DataTable
        Dim conexion As New ConexionBD.ConexionBD()
        Dim dtConsulta As New DataTable
        If conexion.Conectar() Then
            Dim sentencia As String
            sentencia = "SELECT TOP 1 * FROM MCOMPROBANTE WHERE COD_CPB=" + _
                "(SELECT TOP 1 COD_CPB FROM MCOMPROBANTE WHERE COD_CPB<@COD_CPB " + _
                "GROUP BY COD_CPB ORDER BY COD_CPB DESC)"
            conexion.ParametroEntrada = New List(Of DbParameter)
            conexion.ParametroEntrada.Add(ConexionBD.ConexionBD.ParamInString(conexion, _
                                                                              mComp.COD_CPB, "COD_CPB"))
            dtConsulta = conexion.Consulta(sentencia)
            conexion.Desconectar()
        End If
        Return dtConsulta
    End Function

    Public Shared Function ConsxCpbTCpbFecha(mComp As MComprobante) As DataTable
        Dim conexion As New ConexionBD.ConexionBD()
        Dim dtConsulta As New DataTable
        If conexion.Conectar() Then
            Dim sentencia As String
            sentencia = "SELECT TOP 1 * FROM MCOMPROBANTE WHERE COD_CPB=@COD_CPB and " + _
                "COD_TIPO_CPB=@COD_TIPO_CPB and FECHA_CPB BETWEEN '" + mComp.FECHA_CPB + "' AND '" + mComp.FECHA_CPB + "'"
            conexion.ParametroEntrada = New List(Of DbParameter)
            conexion.ParametroEntrada.Add(ConexionBD.ConexionBD.ParamInString(conexion, _
                                                                              mComp.COD_CPB, "COD_CPB"))
            conexion.ParametroEntrada.Add(ConexionBD.ConexionBD.ParamInString(conexion, _
                                                                              mComp.COD_TIPO_CPB, "COD_TIPO_CPB"))
            'conexion.ParametroEntrada.Add(ConexionBD.ConexionBD.ParamInFecha(conexion, _
            '                                                                  mComp.FECHA_CPB, "FECHA_CPB"))
            dtConsulta = conexion.Consulta(sentencia)
            conexion.Desconectar()
        End If
        Return dtConsulta
    End Function

End Class
